﻿@model SrBBSPro.Entities.Project
@{
    ViewBag.Title = "添加项目";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var IsAdmin = Convert.ToBoolean(Session["IsAdmin"]);
}
<div class="main layui-clear">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>项目人员</legend>
    </fieldset>
    <div class="layui-form layui-form-pane">
        <div class="wrap">
            <table id="roles" lay-filter="roles"></table>
        </div>
        @if (IsAdmin)
        {
            <fieldset class="layui-elem-field">
                <legend>项目人员选择</legend>
                <div class="layui-field-box">
                    <form method="post">
                        @Html.AntiForgeryToken()
                        <input type="hidden" id="Id" name="Id" value="@Model.Id" />
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">工号</label>
                                <div class="layui-input-block" style="width:120px;">
                                    <input id="No" name="No" class="layui-input" readonly="readonly" required lay-verify="required" />
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">姓名</label>
                                <div class="layui-input-block" style="width:120px;">
                                    <input id="StaffName" name="StaffName" class="layui-input" readonly="readonly" required lay-verify="required" />
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">部门</label>
                                <div class="layui-input-block" style="width:120px;">
                                    <select id="Depart" name="Depart">
                                        <option value="">请选择</option>
                                        <option value="1">一部</option>
                                        <option value="2">二部</option>
                                        <option value="3">三部</option>
                                        <option value="4">四部</option>
                                        <option value="5">五部</option>
                                        <option value="6">六部</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">职能</label>
                                <div class="layui-input-block" style="width:120px;">
                                    <select id="Role" name="Role" required lay-verify="required">
                                        <option value="1">项目经理</option>
                                        <option value="2">技术开发</option>
                                        <option value="3">工具开发</option>
                                        <option value="4">测试人员</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="addMe">信息提交</button>
                        </div>
                    </form>
                </div>
            </fieldset>
        }
    </div>
    <script type="text/html" id="toolbarDemo">
        <b>@Model.Name 项目成员一览</b>
    </script>
    <script type="text/html" id="StaffNoTpl">
        <a href="#" class="layui-table-link" lay-event="sno">{{d.StaffNo || ''}}</a>
    </script>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
        @if (IsAdmin)
        {
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        }
    </script>
</div>
@section scripts{
    <script>
            layui.use(['jquery', 'form', 'table','common'], function () {
                var table = layui.table;
                var $ = layui.jquery;
                var form = layui.form;
                var pid = '@Model.Id';
                table.render({
                    elem: '#roles'
                    , height: 500
                    , title: '项目成员一览'
                    , page:true
                    , limits: [10]
                    , url: '/Project/MeList?hid=' + pid
                    , autoSort: false
                    , toolbar: '#toolbarDemo'
                    , defaultToolbar: ['filter', 'exports', 'print']
                    , cols: [[
                        { type: 'numbers' }
                        , { field: 'Id', title: 'ID', hide: true }
                        , { field: 'StaffNo', title: '工号' ,width: 100,templet: '#StaffNoTpl'}
                        , { field: 'StaffName', title: '姓名', width: 120 }
                        , { field: 'Depart', title: '所属部门', width: 90, hide: true }
                        , { field: 'DepartName', title: '所属部门', width: 90 }
                        , { field: 'RoleName', title: '角色', width: 120 }
                        , { field: 'DelBtn', fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150 }
                    ]]
                });

                //监听行工具事件
                table.on('tool(roles)', function (obj) {
                    var data = obj.data;
                    if (obj.event === 'del') {
                        layer.confirm('确定要删除该行数据吗？', function (index) {
                            getPost('/Project/DelMember', {
                                pid: data.Id,
                                sno: data.StaffNo
                            }, function (res) {
                                if (res.status == 0) {
                                    layer.msg("删除成功", { icon: 1 });
                                    obj.del();
                                    layer.close(index);
                                    table.reload('roles');
                                } else if (res.status == 1) {
                                    layer.msg(res.msg, { icon: 5 });
                                }
                            });                          
                        });
                    } else if (obj.event === 'sno') {
                        location.href = '/Skill/Detail?UserId=' + data.StaffNo;
                    } else if (obj.event === 'edit') {
                        $("#No").val(data.StaffNo);
                        $("#StaffName").val(data.StaffName);
                        $("#Depart").val(data.Depart);
                        $("#Role").val(data.Role);
                        form.render();
                    }
                });

                form.on('submit(addMe)', function (data) {
                    $.post('@Url.Action("AddMember","Project")', data.field, function (res) {
                        if (res.status==1) {
                            layer.msg(res.msg, { icon: 2 });
                        } else {
                            layer.msg(res.msg, { icon: 1,time:1000 }, function (index) {
                                layer.close(index);
                                table.reload('roles');
                                $("#No").val('');
                                $("#StaffName").val('');
                                form.render();
                            });
                        }
                    }, 'json');
                    return false;
                });

                $("#No").click(function (){
                    var dep = $("#Depart").val();
                    layer.open({
                        type: 2,
                        title: '社员选择',
                        maxmin: false,
                        area: ['400px', '500px'],
                        content: '@Url.Action("List", "Account")',
                        success: function (layero, index) {
                            var iframe = window['layui-layer-iframe' + index];
                            iframe.child(dep);
                        }
                    });
                });
            })
    </script>
}
